// pages/dogCard/dogCard.js
const app = getApp()
//const user = JSON.parse(wx.getStorageSync("user"));
const base_url = app.globalData.base_url;
const util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userid:null,
    navIndex: '0',
    page:{
      p:0,
      psize:10
    },
    dcArr:[],

    familyArr:[],//我的家庭
    collectArr:[],
    scrollTop:0,
    hei:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSystemInfo({
      success: res => {
        let windowHeight = (res.windowHeight * (750 / res.windowWidth));
        this.setData({
          hei: windowHeight - 142 - 32-50
        })
      }
    })
    wx.setNavigationBarTitle({
      title: '狗牌'
    })
    if(options.id){
      this.setData({
        "userid":options.id
      })
    }else{
      this.setData({
        "userid":null
      })
    }
    this.init();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    wx.hideLoading();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  down() {
    if (this.data.navIndex == 0) {
      wx.showLoading({
        title: '加载中',
        success: () => {
          this.setData({
            'page.p': this.data.page.p + 1
          })
          this.init()
        }
      })
    }
  
  },
  up() {    
    if (this.data.navIndex == 0) {
      let refresh = true
      this.setData({
        'page.p': 0
      })
      this.init(refresh)
    } else {
      this.familyInit()
    }

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
  selNav: function(e){//导航 切换
    this.setData({
      'page.p': 0,
      scrollTop: 0
    })
    this.setData({ navIndex : e.currentTarget.dataset.sel});
    if(this.data.navIndex == 1){//我的家庭页面
      wx.hideLoading();
      this.familyInit()
    }
  },
  addMore:function(e){//添加狗牌
    wx.navigateTo({
      url: '../addDogCard/addDogCard'
    })
  },
  detail:function(e){//狗牌详情
      wx.navigateTo({
        url: '../dcDetails/dcDetails?id=' + e.currentTarget.dataset.id
      })
  },
  dels(e){
    let id = e.currentTarget.dataset.id,
      index= e.currentTarget.dataset.index;
    wx.showLoading({
      success: () => {
        wx.request({
          url: base_url + '/Dog/del',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'dog_id': id,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: res => {
            let arr = this.data.dcArr;
            if (res.data.success == '1') {
              arr.splice(index,1);
              wx.showToast({
                title: '移除成功',
                icon: 'success',
                duration: 2000
              })
              this.setData({
                dcArr: arr
              })
            } else {
              wx.showToast({
                title: '移除失败',
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    })
  },
  del(e) {
    let id = e.currentTarget.dataset.id,
      index = e.currentTarget.dataset.index;
    wx.showLoading({
      success: () => {
        wx.request({
          url: base_url + '/Profess/del',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'profess_id': id,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: res => {
            let arr = this.data.familyArr;
            if (res.data.success == '1') {
              arr.splice(index, 1);
              wx.showToast({
                title: '移除成功',
                icon: 'success',
                duration: 2000
              })
              this.setData({
                familyArr: arr
              })
            } else {
              wx.showToast({
                title: '移除失败',
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    })
  },
  remove(e){
    let id = e.currentTarget.dataset.id
    wx.showLoading({
      success: () => {
        wx.request({
          url: base_url + '/Dog/collectOrCancel',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'dog_id': id,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: res => {
            if (res.data.success == '1') {
              let collectArr = this.data.collectArr.filter(v => {
                return !(v.dog_id == id)
              })
              wx.showToast({
                title: '移除成功',
                icon: 'success',
                duration: 2000
              })
              this.setData({
                collectArr: collectArr
              })
            } else {
              wx.showToast({
                title: '移除失败',
                icon: 'none',
                duration: 2000
              })
            }
            wx.hideLoading();
          }
        })
      }
    })
  },
  todetail(e){
    wx.navigateTo({
      url: '../dcDetails/dcDetails?id=' + e.currentTarget.dataset.id + '&userid=' + e.currentTarget.dataset.userid
    })
  },
  
  init(refresh = false){
    wx.showLoading({
      title: '加载中',
      success:()=>{
        let data;
        if(this.data.userid){
          data={
            'user_id':this.data.userid,
            'p': this.data.page.p,
            'psize': this.data.page.psize
          }
        }else{
          data={
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token ,
            'p': this.data.page.p,
            'psize': this.data.page.psize
          }
        }
        wx.request({
          url: base_url + '/Dog/getList',
          data: data,
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: res => {
            wx.hideLoading();
            if (res.data.success == '1') {
              if(res.data.data == null){
                wx.showToast({
                  title: '暂无更多数据',
                  icon: 'none',
                  duration: 2000
                })
                return false
              }
              let arr = [];
            
              refresh == true ? arr = res.data.data : arr = this.data.dcArr.concat(res.data.data);
              for (let  val of arr){
                if (val.yuanbao_sum>=10000){
                  val.All = (val.yuanbao_sum/10000).toFixed(1)
                }else{
                  val.All = val.yuanbao_sum
                }
              }
              this.setData({
                dcArr: arr
              })
              refresh == true && wx.stopPullDownRefresh();
            }else{
              wx.showToast({
                title: '加载失败',
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    })
  },
  familyInit(refresh = false){
    wx.showLoading({
      title: '加载中',
      success:()=>{
        wx.request({
          url: base_url + '/Dog/getFamily',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: res => {
           
            if (res.data.success == '1') {
              
              // let arr = [], brr = [];
              // refresh == true ? arr = res.data.data.family : arr = this.data.familyArr.concat(res.data.data.family);
              // refresh == true ? brr = res.data.data.collect : arr = this.data.collectArr.concat(res.data.data.collect);
              this.setData({
                familyArr: res.data.data.family ,
                collectArr: res.data.data.collect 
              })
              refresh == true && wx.stopPullDownRefresh();
            } else {
              wx.showToast({
                title: '加载失败',
                icon: 'none',
                duration: 2000
              })
            }
            wx.hideLoading();
          }
        })
      }
    })
  },

})